window.onload = function() {
	if ("geolocation" in navigator) {
		var geo_options = {
			enableHighAccuracy : true,
			maximumAge : 30000,
			timeout : 27000
		};
		navigator.geolocation.getCurrentPosition(function(position) {
			var lat = position.coords.latitude;
			var lng = position.coords.longitude;
			doOnLoad(lat, lng, getGridId());
			// getAddress(lat, lng, doOnLoad);//not needed for now
		}, function() {
			alert("This application requires your location to work properly.");
		}, geo_options);
	} else {
		alert("Not able to get your Location.");
	}
};

function getAddress(lat, lng, onSuccess) {
	var latlng = new google.maps.LatLng(lat, lng);
	var geocoder = new google.maps.Geocoder();
	geocoder.geocode({
		'latLng' : latlng
	}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			onSuccess(lat, lng, getGridId(), results[1]);
		} else {
			alert("Geocoder failed due to: " + status);
		}
	});
}

function getGridId(lat, lng) {
	lat = parseInt(lat * EdinburghMapConfig.GRID_DENSITY) / EdinburghMapConfig.GRID_DENSITY;
	lng = parseInt(lng * EdinburghMapConfig.GRID_DENSITY) / EdinburghMapConfig.GRID_DENSITY;
	return lat + "#" + lng;
}
